package com.coderlin1.controller;

import com.coderlin1.entities.Worker;
import com.coderlin1.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/07/30 16:40 <br>
 * @see com.coderlin1.controller <br>
 */
@Controller
@Slf4j
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @RequestMapping("/listWorker")
    public String listWorker(Model model){
        List<Worker> workers = workerService.listWorker();
        model.addAttribute("workerList",workers);

        return "worker/workerList";
    }

    @RequestMapping(value = "/addWorker",method = RequestMethod.POST)
    public String addWorker(Worker worker, MultipartFile fileUpLoad){
        try {
            workerService.addWorker(worker,fileUpLoad);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:/worker/listWorker";
    }

}
